package org.benxiong.iplay.ui.fragment;

import org.benxiong.iplay.R;
import org.benxiong.iplay.util.ToastUtil;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Fragment;
import android.app.ProgressDialog;

public class BaseFragment extends Fragment{
	public static final String CODE_OK = "OK";
	public static final String CODE_SUCCESS = "SUCCESS";
	public static final String CODE_EMPTY = "NOT_FOUND";
	public static final String CODE_DUPLI = "DUPLICATED";
	public static final String CODE_NOT_MYSELF = "NOT_MYSELF";
	
	protected ProgressDialog	mProgressDialog;
	
	protected boolean shouldProcess(JSONObject json) {
		hideLoading();
		String code = null;
		try {
			code = json.getString("code");
			if (CODE_SUCCESS.equals(code) || CODE_OK.equals(code)) {
				return true;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		if (BaseFragment.CODE_EMPTY.equals(code)) {
			ToastUtil.toast(R.string.empty_result);
		} else if (CODE_NOT_MYSELF.equals(code)) {
			ToastUtil.toast(R.string.not_myself);
		} else if (CODE_DUPLI.equals(code)) {
			ToastUtil.toast(R.string.duplicate_operation);
		} else {
			ToastUtil.toast(code);
		}
		return false;
	}
	
	protected void showRequestFailMsg(String msg) {
		ToastUtil.toast(msg);
	}
	
	public void showLoading(int resId) {
		String text = this.getResources().getString(resId);
		showLoading(text);
	}
	
	public void showLoading(String text) {
		if ("".equals(text)||text == null) {
			text = "loading...";
		}
		mProgressDialog = new ProgressDialog(this.getActivity());
		mProgressDialog.setMessage(text);
		mProgressDialog.show();
	}
	
	public void showLoading() {
		showLoading(null);
	}
	
	public void hideLoading() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}
}
